﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Address;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;
using HslCommunication.Reflection;
using HslCommunication.Serial;

namespace HslCommunication.ModBus
{
	/// <summary>
	/// <b>[商业授权]</b> Modbus的虚拟服务器，同时支持Tcp和Rtu的机制，支持线圈，离散输入，寄存器和输入寄存器的读写操作，同时支持掩码写入功能，可以用来当做系统的数据交换池<br />
	/// <b>[Authorization]</b> Modbus virtual server supports Tcp and Rtu mechanisms at the same time, supports read and write operations of coils, discrete inputs, r
	/// egisters and input registers, and supports mask write function, which can be used as a system data exchange pool
	/// </summary>
	/// <remarks>
	/// 可以基于本类实现一个功能复杂的modbus服务器，支持Modbus-Tcp，启动串口后，还支持Modbus-Rtu和Modbus-ASCII，会根据报文进行动态的适配。
	/// <list type="number">
	/// <item>线圈，功能码对应01，05，15</item>
	/// <item>离散输入，功能码对应02</item>
	/// <item>寄存器，功能码对应03，06，16</item>
	/// <item>输入寄存器，功能码对应04，输入寄存器在服务器端可以实现读写的操作</item>
	/// <item>掩码写入，功能码对应22，可以对字寄存器进行位操作</item>
	/// </list>
	/// </remarks>
	/// <example>
	/// 读写的地址格式为富文本地址，具体请参照下面的示例代码。
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Modbus\ModbusTcpServer.cs" region="ModbusTcpServerExample" title="ModbusTcpServer示例" />
	/// </example>
	// Token: 0x020000D2 RID: 210
	public class ModbusTcpServer : NetworkDataServerBase
	{
		/// <summary>
		/// 实例化一个Modbus Tcp及Rtu的服务器，支持数据读写操作
		/// </summary>
		// Token: 0x0600103D RID: 4157 RVA: 0x0005DE5C File Offset: 0x0005C05C
		public ModbusTcpServer()
		{
			this.coilBuffer = new SoftBuffer(65536);
			this.inputBuffer = new SoftBuffer(65536);
			this.registerBuffer = new SoftBuffer(131072);
			this.inputRegisterBuffer = new SoftBuffer(131072);
			this.registerBuffer.IsBoolReverseByWord = true;
			this.inputRegisterBuffer.IsBoolReverseByWord = true;
			this.subscriptions = new List<ModBusMonitorAddress>();
			this.subcriptionHybirdLock = new SimpleHybirdLock();
			base.ByteTransform = new ReverseWordTransform();
			base.WordLength = 1;
			this.serialPort = new SerialPort();
		}

		/// <inheritdoc cref="P:HslCommunication.ModBus.ModbusTcpNet.DataFormat" />
		// Token: 0x17000250 RID: 592
		// (get) Token: 0x0600103E RID: 4158 RVA: 0x0005DF08 File Offset: 0x0005C108
		// (set) Token: 0x0600103F RID: 4159 RVA: 0x00048F41 File Offset: 0x00047141
		public DataFormat DataFormat
		{
			get
			{
				return base.ByteTransform.DataFormat;
			}
			set
			{
				base.ByteTransform.DataFormat = value;
			}
		}

		/// <inheritdoc cref="P:HslCommunication.ModBus.ModbusTcpNet.IsStringReverse" />
		// Token: 0x17000251 RID: 593
		// (get) Token: 0x06001040 RID: 4160 RVA: 0x0005DF28 File Offset: 0x0005C128
		// (set) Token: 0x06001041 RID: 4161 RVA: 0x00048F71 File Offset: 0x00047171
		public bool IsStringReverse
		{
			get
			{
				return base.ByteTransform.IsStringReverseByteWord;
			}
			set
			{
				base.ByteTransform.IsStringReverseByteWord = value;
			}
		}

		/// <inheritdoc cref="P:HslCommunication.ModBus.ModbusTcpNet.Station" />
		// Token: 0x17000252 RID: 594
		// (get) Token: 0x06001042 RID: 4162 RVA: 0x0005DF48 File Offset: 0x0005C148
		// (set) Token: 0x06001043 RID: 4163 RVA: 0x0005DF60 File Offset: 0x0005C160
		public int Station
		{
			get
			{
				return this.station;
			}
			set
			{
				this.station = value;
			}
		}

		/// <inheritdoc />
		// Token: 0x06001044 RID: 4164 RVA: 0x0005DF6C File Offset: 0x0005C16C
		protected override byte[] SaveToBytes()
		{
			byte[] array = new byte[393216];
			Array.Copy(this.coilBuffer.GetBytes(), 0, array, 0, 65536);
			Array.Copy(this.inputBuffer.GetBytes(), 0, array, 65536, 65536);
			Array.Copy(this.registerBuffer.GetBytes(), 0, array, 131072, 131072);
			Array.Copy(this.inputRegisterBuffer.GetBytes(), 0, array, 262144, 131072);
			return array;
		}

		/// <inheritdoc />
		// Token: 0x06001045 RID: 4165 RVA: 0x0005DFFC File Offset: 0x0005C1FC
		protected override void LoadFromBytes(byte[] content)
		{
			bool flag = content.Length < 393216;
			if (flag)
			{
				throw new Exception("File is not correct");
			}
			this.coilBuffer.SetBytes(content, 0, 0, 65536);
			this.inputBuffer.SetBytes(content, 65536, 0, 65536);
			this.registerBuffer.SetBytes(content, 131072, 0, 131072);
			this.inputRegisterBuffer.SetBytes(content, 262144, 0, 131072);
		}

		/// <summary>
		/// 读取地址的线圈的通断情况
		/// </summary>
		/// <param name="address">起始地址，示例："100"</param>
		/// <returns><c>True</c>或是<c>False</c></returns>
		/// <exception cref="T:System.IndexOutOfRangeException"></exception>
		// Token: 0x06001046 RID: 4166 RVA: 0x0005E080 File Offset: 0x0005C280
		public bool ReadCoil(string address)
		{
			ushort index = ushort.Parse(address);
			return this.coilBuffer.GetByte((int)index) > 0;
		}

		/// <summary>
		/// 批量读取地址的线圈的通断情况
		/// </summary>
		/// <param name="address">起始地址，示例："100"</param>
		/// <param name="length">读取长度</param>
		/// <returns><c>True</c>或是<c>False</c></returns>
		/// <exception cref="T:System.IndexOutOfRangeException"></exception>
		// Token: 0x06001047 RID: 4167 RVA: 0x0005E0A8 File Offset: 0x0005C2A8
		public bool[] ReadCoil(string address, ushort length)
		{
			ushort index = ushort.Parse(address);
			return Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.coilBuffer.GetBytes((int)index, (int)length), (byte m) => m > 0));
		}

		/// <summary>
		/// 写入线圈的通断值
		/// </summary>
		/// <param name="address">起始地址，示例："100"</param>
		/// <param name="data">是否通断</param>
		/// <returns><c>True</c>或是<c>False</c></returns>
		/// <exception cref="T:System.IndexOutOfRangeException"></exception>
		// Token: 0x06001048 RID: 4168 RVA: 0x0005E0F8 File Offset: 0x0005C2F8
		public void WriteCoil(string address, bool data)
		{
			ushort index = ushort.Parse(address);
			this.coilBuffer.SetValue(data ? 1 : 0, (int)index);
		}

		/// <summary>
		/// 写入线圈数组的通断值
		/// </summary>
		/// <param name="address">起始地址，示例："100"</param>
		/// <param name="data">是否通断</param>
		/// <returns><c>True</c>或是<c>False</c></returns>
		/// <exception cref="T:System.IndexOutOfRangeException"></exception>
		// Token: 0x06001049 RID: 4169 RVA: 0x0005E124 File Offset: 0x0005C324
		public void WriteCoil(string address, bool[] data)
		{
			bool flag = data == null;
			if (!flag)
			{
				ushort destIndex = ushort.Parse(address);
				this.coilBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(data, (bool m) => m ? 1 : 0)), (int)destIndex);
			}
		}

		/// <summary>
		/// 读取地址的离散线圈的通断情况
		/// </summary>
		/// <param name="address">起始地址，示例："100"</param>
		/// <returns><c>True</c>或是<c>False</c></returns>
		/// <exception cref="T:System.IndexOutOfRangeException"></exception>
		// Token: 0x0600104A RID: 4170 RVA: 0x0005E17C File Offset: 0x0005C37C
		public bool ReadDiscrete(string address)
		{
			ushort index = ushort.Parse(address);
			return this.inputBuffer.GetByte((int)index) > 0;
		}

		/// <summary>
		/// 批量读取地址的离散线圈的通断情况
		/// </summary>
		/// <param name="address">起始地址，示例："100"</param>
		/// <param name="length">读取长度</param>
		/// <returns><c>True</c>或是<c>False</c></returns>
		/// <exception cref="T:System.IndexOutOfRangeException"></exception>
		// Token: 0x0600104B RID: 4171 RVA: 0x0005E1A4 File Offset: 0x0005C3A4
		public bool[] ReadDiscrete(string address, ushort length)
		{
			ushort index = ushort.Parse(address);
			return Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.inputBuffer.GetBytes((int)index, (int)length), (byte m) => m > 0));
		}

		/// <summary>
		/// 写入离散线圈的通断值
		/// </summary>
		/// <param name="address">起始地址，示例："100"</param>
		/// <param name="data">是否通断</param>
		/// <exception cref="T:System.IndexOutOfRangeException"></exception>
		// Token: 0x0600104C RID: 4172 RVA: 0x0005E1F4 File Offset: 0x0005C3F4
		public void WriteDiscrete(string address, bool data)
		{
			ushort index = ushort.Parse(address);
			this.inputBuffer.SetValue(data ? 1 : 0, (int)index);
		}

		/// <summary>
		/// 写入离散线圈数组的通断值
		/// </summary>
		/// <param name="address">起始地址，示例："100"</param>
		/// <param name="data">是否通断</param>
		/// <exception cref="T:System.IndexOutOfRangeException"></exception>
		// Token: 0x0600104D RID: 4173 RVA: 0x0005E220 File Offset: 0x0005C420
		public void WriteDiscrete(string address, bool[] data)
		{
			bool flag = data == null;
			if (!flag)
			{
				ushort destIndex = ushort.Parse(address);
				this.inputBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(data, (bool m) => m ? 1 : 0)), (int)destIndex);
			}
		}

		/// <inheritdoc cref="M:HslCommunication.ModBus.ModbusTcpNet.Read(System.String,System.UInt16)" />
		// Token: 0x0600104E RID: 4174 RVA: 0x0005E278 File Offset: 0x0005C478
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<ModbusAddress> operateResult = ModbusInfo.AnalysisAddress(address, (byte)this.Station, true, 3);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content.Function == 3;
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult<byte[]>(this.registerBuffer.GetBytes((int)(operateResult.Content.Address * 2), (int)(length * 2)));
				}
				else
				{
					bool flag3 = operateResult.Content.Function == 4;
					if (flag3)
					{
						result = OperateResult.CreateSuccessResult<byte[]>(this.inputRegisterBuffer.GetBytes((int)(operateResult.Content.Address * 2), (int)(length * 2)));
					}
					else
					{
						result = new OperateResult<byte[]>(StringResources.Language.NotSupportedDataType);
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.ModBus.ModbusTcpNet.Write(System.String,System.Byte[])" />
		// Token: 0x0600104F RID: 4175 RVA: 0x0005E32C File Offset: 0x0005C52C
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<ModbusAddress> operateResult = ModbusInfo.AnalysisAddress(address, (byte)this.Station, true, 3);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content.Function == 3;
				if (flag2)
				{
					this.registerBuffer.SetBytes(value, (int)(operateResult.Content.Address * 2));
					result = OperateResult.CreateSuccessResult();
				}
				else
				{
					bool flag3 = operateResult.Content.Function == 4;
					if (flag3)
					{
						this.inputRegisterBuffer.SetBytes(value, (int)(operateResult.Content.Address * 2));
						result = OperateResult.CreateSuccessResult();
					}
					else
					{
						result = new OperateResult<byte[]>(StringResources.Language.NotSupportedDataType);
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.ModBus.ModbusTcpNet.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06001050 RID: 4176 RVA: 0x0005E3E0 File Offset: 0x0005C5E0
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<ModbusAddress> operateResult = ModbusInfo.AnalysisAddress(address, (byte)this.Station, true, 1);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content.Function == 1;
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.coilBuffer.GetBytes((int)operateResult.Content.Address, (int)length), (byte m) => m > 0)));
				}
				else
				{
					bool flag3 = operateResult.Content.Function == 2;
					if (flag3)
					{
						result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.inputBuffer.GetBytes((int)operateResult.Content.Address, (int)length), (byte m) => m > 0)));
					}
					else
					{
						result = new OperateResult<bool[]>(StringResources.Language.NotSupportedDataType);
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.ModBus.ModbusTcpNet.Write(System.String,System.Boolean[])" />
		// Token: 0x06001051 RID: 4177 RVA: 0x0005E4E0 File Offset: 0x0005C6E0
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] value)
		{
			OperateResult<ModbusAddress> operateResult = ModbusInfo.AnalysisAddress(address, (byte)this.Station, true, 1);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content.Function == 1;
				if (flag2)
				{
					this.coilBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(value, (bool m) => m ? 1 : 0)), (int)operateResult.Content.Address);
					result = OperateResult.CreateSuccessResult();
				}
				else
				{
					bool flag3 = operateResult.Content.Function == 2;
					if (flag3)
					{
						this.inputBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(value, (bool m) => m ? 1 : 0)), (int)operateResult.Content.Address);
						result = OperateResult.CreateSuccessResult();
					}
					else
					{
						result = new OperateResult<byte[]>(StringResources.Language.NotSupportedDataType);
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.ModBus.ModbusTcpNet.Write(System.String,System.Boolean)" />
		// Token: 0x06001052 RID: 4178 RVA: 0x0005E5E8 File Offset: 0x0005C7E8
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			bool flag = address.IndexOf('.') < 0;
			OperateResult result;
			if (flag)
			{
				result = base.Write(address, value);
			}
			else
			{
				try
				{
					int num = Convert.ToInt32(address.Substring(address.IndexOf('.') + 1));
					address = address.Substring(0, address.IndexOf('.'));
					OperateResult<ModbusAddress> operateResult = ModbusInfo.AnalysisAddress(address, (byte)this.Station, true, 3);
					bool flag2 = !operateResult.IsSuccess;
					if (flag2)
					{
						result = operateResult;
					}
					else
					{
						num = (int)(operateResult.Content.Address * 16) + num;
						bool flag3 = operateResult.Content.Function == 3;
						if (flag3)
						{
							this.registerBuffer.SetBool(value, num);
							result = OperateResult.CreateSuccessResult();
						}
						else
						{
							bool flag4 = operateResult.Content.Function == 4;
							if (flag4)
							{
								this.inputRegisterBuffer.SetBool(value, num);
								result = OperateResult.CreateSuccessResult();
							}
							else
							{
								result = new OperateResult(StringResources.Language.NotSupportedDataType);
							}
						}
					}
				}
				catch (Exception ex)
				{
					result = new OperateResult(ex.Message);
				}
			}
			return result;
		}

		/// <summary>
		/// 写入寄存器数据，指定字节数据
		/// </summary>
		/// <param name="address">起始地址，示例："100"，如果是输入寄存器："x=4;100"</param>
		/// <param name="high">高位数据</param>
		/// <param name="low">地位数据</param>
		// Token: 0x06001053 RID: 4179 RVA: 0x0005E700 File Offset: 0x0005C900
		public void Write(string address, byte high, byte low)
		{
			this.Write(address, new byte[]
			{
				high,
				low
			});
		}

		/// <inheritdoc />
		// Token: 0x06001054 RID: 4180 RVA: 0x0005E718 File Offset: 0x0005C918
		protected override void ThreadPoolLoginAfterClientCheck(Socket socket, IPEndPoint endPoint)
		{
			AppSession appSession = new AppSession();
			appSession.IpEndPoint = endPoint;
			appSession.WorkSocket = socket;
			bool isSuccess = socket.BeginReceiveResult(new AsyncCallback(this.SocketAsyncCallBack), appSession).IsSuccess;
			if (isSuccess)
			{
				base.AddClient(appSession);
			}
			else
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteDebug(this.ToString(), string.Format(StringResources.Language.ClientOfflineInfo, endPoint));
				}
			}
		}

		// Token: 0x06001055 RID: 4181 RVA: 0x0005E78C File Offset: 0x0005C98C
		[DebuggerStepThrough]
		private void SocketAsyncCallBack(IAsyncResult ar)
		{
			ModbusTcpServer.<SocketAsyncCallBack>d__27 <SocketAsyncCallBack>d__ = new ModbusTcpServer.<SocketAsyncCallBack>d__27();
			<SocketAsyncCallBack>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
			<SocketAsyncCallBack>d__.<>4__this = this;
			<SocketAsyncCallBack>d__.ar = ar;
			<SocketAsyncCallBack>d__.<>1__state = -1;
			<SocketAsyncCallBack>d__.<>t__builder.Start<ModbusTcpServer.<SocketAsyncCallBack>d__27>(ref <SocketAsyncCallBack>d__);
		}

		/// <summary>
		/// 创建特殊的功能标识，然后返回该信息<br />
		/// Create a special feature ID and return this information
		/// </summary>
		/// <param name="modbusCore">modbus核心报文</param>
		/// <param name="error">错误码</param>
		/// <returns>携带错误码的modbus报文</returns>
		// Token: 0x06001056 RID: 4182 RVA: 0x0005E7CC File Offset: 0x0005C9CC
		private byte[] CreateExceptionBack(byte[] modbusCore, byte error)
		{
			return new byte[]
			{
				modbusCore[0],
				modbusCore[1] + 128,
				error
			};
		}

		/// <summary>
		/// 创建返回消息<br />
		/// Create return message
		/// </summary>
		/// <param name="modbusCore">modbus核心报文</param>
		/// <param name="content">返回的实际数据内容</param>
		/// <returns>携带内容的modbus报文</returns>
		// Token: 0x06001057 RID: 4183 RVA: 0x0005E7EB File Offset: 0x0005C9EB
		private byte[] CreateReadBack(byte[] modbusCore, byte[] content)
		{
			return SoftBasic.SpliceArray<byte>(new byte[][]
			{
				new byte[]
				{
					modbusCore[0],
					modbusCore[1],
					(byte)content.Length
				},
				content
			});
		}

		/// <summary>
		/// 创建写入成功的反馈信号<br />
		/// Create feedback signal for successful write
		/// </summary>
		/// <param name="modbus">modbus核心报文</param>
		/// <returns>携带成功写入的信息</returns>
		// Token: 0x06001058 RID: 4184 RVA: 0x0005E818 File Offset: 0x0005CA18
		private byte[] CreateWriteBack(byte[] modbus)
		{
			return modbus.SelectBegin(6);
		}

		// Token: 0x06001059 RID: 4185 RVA: 0x0005E824 File Offset: 0x0005CA24
		private byte[] ReadCoilBack(byte[] modbus, string addressHead)
		{
			byte[] result;
			try
			{
				ushort num = base.ByteTransform.TransUInt16(modbus, 2);
				ushort num2 = base.ByteTransform.TransUInt16(modbus, 4);
				bool flag = (int)(num + num2) > 65536;
				if (flag)
				{
					result = this.CreateExceptionBack(modbus, 2);
				}
				else
				{
					bool flag2 = num2 > 2040;
					if (flag2)
					{
						result = this.CreateExceptionBack(modbus, 3);
					}
					else
					{
						bool[] content = this.ReadBool(addressHead + num.ToString(), num2).Content;
						result = this.CreateReadBack(modbus, SoftBasic.BoolArrayToByte(content));
					}
				}
			}
			catch (Exception ex)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), StringResources.Language.ModbusTcpReadCoilException, ex);
				}
				result = this.CreateExceptionBack(modbus, 4);
			}
			return result;
		}

		// Token: 0x0600105A RID: 4186 RVA: 0x0005E8F4 File Offset: 0x0005CAF4
		private byte[] ReadRegisterBack(byte[] modbus, string addressHead)
		{
			byte[] result;
			try
			{
				ushort num = base.ByteTransform.TransUInt16(modbus, 2);
				ushort num2 = base.ByteTransform.TransUInt16(modbus, 4);
				bool flag = (int)(num + num2) > 65536;
				if (flag)
				{
					result = this.CreateExceptionBack(modbus, 2);
				}
				else
				{
					bool flag2 = num2 > 127;
					if (flag2)
					{
						result = this.CreateExceptionBack(modbus, 3);
					}
					else
					{
						byte[] content = this.Read(addressHead + num.ToString(), num2).Content;
						result = this.CreateReadBack(modbus, content);
					}
				}
			}
			catch (Exception ex)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), StringResources.Language.ModbusTcpReadRegisterException, ex);
				}
				result = this.CreateExceptionBack(modbus, 4);
			}
			return result;
		}

		// Token: 0x0600105B RID: 4187 RVA: 0x0005E9BC File Offset: 0x0005CBBC
		private byte[] WriteOneCoilBack(byte[] modbus)
		{
			byte[] result;
			try
			{
				ushort num = base.ByteTransform.TransUInt16(modbus, 2);
				bool flag = modbus[4] == byte.MaxValue && modbus[5] == 0;
				if (flag)
				{
					this.Write(num.ToString(), true);
				}
				else
				{
					bool flag2 = modbus[4] == 0 && modbus[5] == 0;
					if (flag2)
					{
						this.Write(num.ToString(), false);
					}
				}
				result = this.CreateWriteBack(modbus);
			}
			catch (Exception ex)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), StringResources.Language.ModbusTcpWriteCoilException, ex);
				}
				result = this.CreateExceptionBack(modbus, 4);
			}
			return result;
		}

		// Token: 0x0600105C RID: 4188 RVA: 0x0005EA70 File Offset: 0x0005CC70
		private byte[] WriteOneRegisterBack(byte[] modbus)
		{
			byte[] result;
			try
			{
				ushort address = base.ByteTransform.TransUInt16(modbus, 2);
				short content = base.ReadInt16(address.ToString()).Content;
				this.Write(address.ToString(), modbus[4], modbus[5]);
				short content2 = base.ReadInt16(address.ToString()).Content;
				this.OnRegisterBeforWrite(address, content, content2);
				result = this.CreateWriteBack(modbus);
			}
			catch (Exception ex)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), StringResources.Language.ModbusTcpWriteRegisterException, ex);
				}
				result = this.CreateExceptionBack(modbus, 4);
			}
			return result;
		}

		// Token: 0x0600105D RID: 4189 RVA: 0x0005EB20 File Offset: 0x0005CD20
		private byte[] WriteCoilsBack(byte[] modbus)
		{
			byte[] result;
			try
			{
				ushort num = base.ByteTransform.TransUInt16(modbus, 2);
				ushort num2 = base.ByteTransform.TransUInt16(modbus, 4);
				bool flag = (int)(num + num2) > 65536;
				if (flag)
				{
					result = this.CreateExceptionBack(modbus, 2);
				}
				else
				{
					bool flag2 = num2 > 2040;
					if (flag2)
					{
						result = this.CreateExceptionBack(modbus, 3);
					}
					else
					{
						this.Write(num.ToString(), modbus.RemoveBegin(7).ToBoolArray((int)num2));
						result = this.CreateWriteBack(modbus);
					}
				}
			}
			catch (Exception ex)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), StringResources.Language.ModbusTcpWriteCoilException, ex);
				}
				result = this.CreateExceptionBack(modbus, 4);
			}
			return result;
		}

		// Token: 0x0600105E RID: 4190 RVA: 0x0005EBE8 File Offset: 0x0005CDE8
		private byte[] WriteRegisterBack(byte[] modbus)
		{
			byte[] result;
			try
			{
				ushort num = base.ByteTransform.TransUInt16(modbus, 2);
				ushort num2 = base.ByteTransform.TransUInt16(modbus, 4);
				bool flag = (int)(num + num2) > 65536;
				if (flag)
				{
					result = this.CreateExceptionBack(modbus, 2);
				}
				else
				{
					bool flag2 = num2 > 127;
					if (flag2)
					{
						result = this.CreateExceptionBack(modbus, 3);
					}
					else
					{
						MonitorAddress[] array = new MonitorAddress[(int)num2];
						for (ushort num3 = 0; num3 < num2; num3 += 1)
						{
							short content = base.ReadInt16(((int)(num + num3)).ToString()).Content;
							this.Write(((int)(num + num3)).ToString(), modbus[(int)(2 * num3 + 7)], modbus[(int)(2 * num3 + 8)]);
							short content2 = base.ReadInt16(((int)(num + num3)).ToString()).Content;
							array[(int)num3] = new MonitorAddress
							{
								Address = num + num3,
								ValueOrigin = content,
								ValueNew = content2
							};
						}
						for (int i = 0; i < array.Length; i++)
						{
							this.OnRegisterBeforWrite(array[i].Address, array[i].ValueOrigin, array[i].ValueNew);
						}
						result = this.CreateWriteBack(modbus);
					}
				}
			}
			catch (Exception ex)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), StringResources.Language.ModbusTcpWriteRegisterException, ex);
				}
				result = this.CreateExceptionBack(modbus, 4);
			}
			return result;
		}

		// Token: 0x0600105F RID: 4191 RVA: 0x0005ED9C File Offset: 0x0005CF9C
		private byte[] WriteMaskRegisterBack(byte[] modbus)
		{
			byte[] result;
			try
			{
				ushort address = base.ByteTransform.TransUInt16(modbus, 2);
				int num = (int)base.ByteTransform.TransUInt16(modbus, 4);
				int num2 = (int)base.ByteTransform.TransUInt16(modbus, 6);
				int content = (int)base.ReadInt16(address.ToString()).Content;
				short num3 = (short)((content & num) | num2);
				this.Write(address.ToString(), num3);
				MonitorAddress monitorAddress = new MonitorAddress
				{
					Address = address,
					ValueOrigin = (short)content,
					ValueNew = num3
				};
				this.OnRegisterBeforWrite(monitorAddress.Address, monitorAddress.ValueOrigin, monitorAddress.ValueNew);
				result = modbus;
			}
			catch (Exception ex)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), StringResources.Language.ModbusTcpWriteRegisterException, ex);
				}
				result = this.CreateExceptionBack(modbus, 4);
			}
			return result;
		}

		/// <summary>
		/// 新增一个数据监视的任务，针对的是寄存器地址的数据<br />
		/// Added a data monitoring task for data at register addresses
		/// </summary>
		/// <param name="monitor">监视地址对象</param>
		// Token: 0x06001060 RID: 4192 RVA: 0x0005EE8C File Offset: 0x0005D08C
		public void AddSubcription(ModBusMonitorAddress monitor)
		{
			this.subcriptionHybirdLock.Enter();
			this.subscriptions.Add(monitor);
			this.subcriptionHybirdLock.Leave();
		}

		/// <summary>
		/// 移除一个数据监视的任务<br />
		/// Remove a data monitoring task
		/// </summary>
		/// <param name="monitor">监视地址对象</param>
		// Token: 0x06001061 RID: 4193 RVA: 0x0005EEB4 File Offset: 0x0005D0B4
		public void RemoveSubcrption(ModBusMonitorAddress monitor)
		{
			this.subcriptionHybirdLock.Enter();
			this.subscriptions.Remove(monitor);
			this.subcriptionHybirdLock.Leave();
		}

		/// <summary>
		/// 在数据变更后，进行触发是否产生订阅<br />
		/// Whether to generate a subscription after triggering data changes
		/// </summary>
		/// <param name="address">数据地址</param>
		/// <param name="before">修改之前的数</param>
		/// <param name="after">修改之后的数</param>
		// Token: 0x06001062 RID: 4194 RVA: 0x0005EEDC File Offset: 0x0005D0DC
		private void OnRegisterBeforWrite(ushort address, short before, short after)
		{
			this.subcriptionHybirdLock.Enter();
			for (int i = 0; i < this.subscriptions.Count; i++)
			{
				bool flag = this.subscriptions[i].Address == address;
				if (flag)
				{
					this.subscriptions[i].SetValue(after);
					bool flag2 = before != after;
					if (flag2)
					{
						this.subscriptions[i].SetChangeValue(before, after);
					}
				}
			}
			this.subcriptionHybirdLock.Leave();
		}

		/// <summary>
		/// 检测当前的Modbus接收的指定是否是合法的<br />
		/// Check if the current Modbus received designation is valid
		/// </summary>
		/// <param name="buffer">缓存数据</param>
		/// <returns>是否合格</returns>
		// Token: 0x06001063 RID: 4195 RVA: 0x0005EF6C File Offset: 0x0005D16C
		private bool CheckModbusMessageLegal(byte[] buffer)
		{
			byte b = buffer[1];
			byte b2 = b;
			bool flag;
			if (b2 - 1 > 5)
			{
				if (b2 - 15 > 1)
				{
					flag = (b2 != 22 || buffer.Length == 8);
				}
				else
				{
					flag = (buffer.Length > 6 && (int)buffer[6] == buffer.Length - 7);
				}
			}
			else
			{
				flag = (buffer.Length == 6);
			}
			bool flag2 = !flag;
			if (flag2)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteError(this.ToString(), "Receive Nosense Modbus-rtu : " + buffer.ToHexString(' '));
				}
			}
			return flag;
		}

		/// <summary>
		/// Modbus核心数据交互方法，允许重写自己来实现，报文只剩下核心的Modbus信息，去除了MPAB报头信息<br />
		/// The Modbus core data interaction method allows you to rewrite it to achieve the message. 
		/// Only the core Modbus information is left in the message, and the MPAB header information is removed.
		/// </summary>
		/// <param name="modbusCore">核心的Modbus报文</param>
		/// <returns>进行数据交互之后的结果</returns>
		// Token: 0x06001064 RID: 4196 RVA: 0x0005F000 File Offset: 0x0005D200
		protected virtual byte[] ReadFromModbusCore(byte[] modbusCore)
		{
			byte b = modbusCore[1];
			byte b2 = b;
			if (b2 <= 15)
			{
				switch (b2)
				{
				case 1:
					return this.ReadCoilBack(modbusCore, string.Empty);
				case 2:
					return this.ReadCoilBack(modbusCore, "x=2;");
				case 3:
					return this.ReadRegisterBack(modbusCore, string.Empty);
				case 4:
					return this.ReadRegisterBack(modbusCore, "x=4;");
				case 5:
					return this.WriteOneCoilBack(modbusCore);
				case 6:
					return this.WriteOneRegisterBack(modbusCore);
				default:
					if (b2 == 15)
					{
						return this.WriteCoilsBack(modbusCore);
					}
					break;
				}
			}
			else
			{
				if (b2 == 16)
				{
					return this.WriteRegisterBack(modbusCore);
				}
				if (b2 == 22)
				{
					return this.WriteMaskRegisterBack(modbusCore);
				}
			}
			return this.CreateExceptionBack(modbusCore, 1);
		}

		/// <summary>
		/// 启动modbus-rtu的从机服务，使用默认的参数进行初始化串口，9600波特率，8位数据位，无奇偶校验，1位停止位<br />
		/// Start the slave service of modbus-rtu, initialize the serial port with default parameters, 9600 baud rate, 8 data bits, no parity, 1 stop bit
		/// </summary>
		/// <param name="com">串口信息</param>
		// Token: 0x06001065 RID: 4197 RVA: 0x0005F0CE File Offset: 0x0005D2CE
		public void StartModbusRtu(string com)
		{
			this.StartModbusRtu(com, 9600);
		}

		/// <summary>
		/// 启动modbus-rtu的从机服务，使用默认的参数进行初始化串口，8位数据位，无奇偶校验，1位停止位<br />
		/// Start the slave service of modbus-rtu, initialize the serial port with default parameters, 8 data bits, no parity, 1 stop bit
		/// </summary>
		/// <param name="com">串口信息</param>
		/// <param name="baudRate">波特率</param>
		// Token: 0x06001066 RID: 4198 RVA: 0x0005F0E0 File Offset: 0x0005D2E0
		public void StartModbusRtu(string com, int baudRate)
		{
			this.StartModbusRtu(delegate(SerialPort sp)
			{
				sp.PortName = com;
				sp.BaudRate = baudRate;
				sp.DataBits = 8;
				sp.Parity = Parity.None;
				sp.StopBits = StopBits.One;
			});
		}

		/// <summary>
		/// 启动modbus-rtu的从机服务，使用自定义的初始化方法初始化串口的参数<br />
		/// Start the slave service of modbus-rtu and initialize the parameters of the serial port using a custom initialization method
		/// </summary>
		/// <param name="inni">初始化信息的委托</param>
		// Token: 0x06001067 RID: 4199 RVA: 0x0005F118 File Offset: 0x0005D318
		public void StartModbusRtu(Action<SerialPort> inni)
		{
			bool flag = !this.serialPort.IsOpen;
			if (flag)
			{
				if (inni != null)
				{
					inni(this.serialPort);
				}
				this.serialPort.ReadBufferSize = 1024;
				this.serialPort.ReceivedBytesThreshold = 1;
				this.serialPort.Open();
				this.serialPort.DataReceived += this.SerialPort_DataReceived;
			}
		}

		/// <summary>
		/// 关闭modbus-rtu的串口对象<br />
		/// Close the serial port object of modbus-rtu
		/// </summary>
		// Token: 0x06001068 RID: 4200 RVA: 0x0005F190 File Offset: 0x0005D390
		public void CloseModbusRtu()
		{
			bool isOpen = this.serialPort.IsOpen;
			if (isOpen)
			{
				this.serialPort.Close();
			}
		}

		/// <summary>
		/// 接收到串口数据的时候触发
		/// </summary>
		/// <param name="sender">串口对象</param>
		/// <param name="e">消息</param>
		// Token: 0x06001069 RID: 4201 RVA: 0x0005F1BC File Offset: 0x0005D3BC
		private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{
			int num = 0;
			byte[] array = new byte[1024];
			bool flag;
			do
			{
				Thread.Sleep(20);
				int num2 = this.serialPort.Read(array, num, this.serialPort.BytesToRead);
				num += num2;
				flag = (num2 == 0);
			}
			while (!flag);
			bool flag2 = num == 0;
			if (!flag2)
			{
				byte[] array2 = array.SelectBegin(num);
				bool flag3 = array2.Length < 3;
				if (flag3)
				{
					ILogNet logNet = base.LogNet;
					if (logNet != null)
					{
						logNet.WriteError(this.ToString(), "[" + this.serialPort.PortName + "] Uknown Data：" + array2.ToHexString(' '));
					}
				}
				else
				{
					bool flag4 = array2[0] != 58;
					if (flag4)
					{
						ILogNet logNet2 = base.LogNet;
						if (logNet2 != null)
						{
							logNet2.WriteDebug(this.ToString(), string.Concat(new string[]
							{
								"[",
								this.serialPort.PortName,
								"] Rtu ",
								StringResources.Language.Receive,
								"：",
								array2.ToHexString(' ')
							}));
						}
						bool flag5 = SoftCRC16.CheckCRC16(array2);
						if (flag5)
						{
							byte[] array3 = array2.RemoveLast(2);
							bool flag6 = !this.CheckModbusMessageLegal(array3);
							if (!flag6)
							{
								bool flag7 = this.station >= 0 && this.station != (int)array3[0];
								if (flag7)
								{
									ILogNet logNet3 = base.LogNet;
									if (logNet3 != null)
									{
										logNet3.WriteError(this.ToString(), "[" + this.serialPort.PortName + "] Station not match Modbus-rtu : " + array2.ToHexString(' '));
									}
								}
								else
								{
									byte[] array4 = ModbusInfo.PackCommandToRtu(this.ReadFromModbusCore(array3));
									this.serialPort.Write(array4, 0, array4.Length);
									ILogNet logNet4 = base.LogNet;
									if (logNet4 != null)
									{
										logNet4.WriteDebug(this.ToString(), string.Concat(new string[]
										{
											"[",
											this.serialPort.PortName,
											"] Rtu ",
											StringResources.Language.Send,
											"：",
											array4.ToHexString(' ')
										}));
									}
									bool isStarted = base.IsStarted;
									if (isStarted)
									{
										base.RaiseDataReceived(array2);
									}
								}
							}
						}
						else
						{
							ILogNet logNet5 = base.LogNet;
							if (logNet5 != null)
							{
								logNet5.WriteWarn("[" + this.serialPort.PortName + "] CRC Check Failed : " + array2.ToHexString(' '));
							}
						}
					}
					else
					{
						ILogNet logNet6 = base.LogNet;
						if (logNet6 != null)
						{
							logNet6.WriteDebug(this.ToString(), string.Concat(new string[]
							{
								"[",
								this.serialPort.PortName,
								"] Ascii ",
								StringResources.Language.Receive,
								"：",
								Encoding.ASCII.GetString(array2.RemoveLast(2))
							}));
						}
						OperateResult<byte[]> operateResult = ModbusInfo.TransAsciiPackCommandToCore(array2);
						bool flag8 = !operateResult.IsSuccess;
						if (flag8)
						{
							ILogNet logNet7 = base.LogNet;
							if (logNet7 != null)
							{
								logNet7.WriteError(this.ToString(), operateResult.Message);
							}
						}
						else
						{
							byte[] content = operateResult.Content;
							bool flag9 = !this.CheckModbusMessageLegal(content);
							if (!flag9)
							{
								bool flag10 = this.station >= 0 && this.station != (int)content[0];
								if (flag10)
								{
									ILogNet logNet8 = base.LogNet;
									if (logNet8 != null)
									{
										logNet8.WriteError(this.ToString(), "[" + this.serialPort.PortName + "] Station not match Modbus-Ascii : " + Encoding.ASCII.GetString(array2.RemoveLast(2)));
									}
								}
								else
								{
									byte[] array5 = ModbusInfo.TransModbusCoreToAsciiPackCommand(this.ReadFromModbusCore(content));
									this.serialPort.Write(array5, 0, array5.Length);
									ILogNet logNet9 = base.LogNet;
									if (logNet9 != null)
									{
										logNet9.WriteDebug(this.ToString(), string.Concat(new string[]
										{
											"[",
											this.serialPort.PortName,
											"] Ascii ",
											StringResources.Language.Send,
											"：",
											Encoding.ASCII.GetString(array5.RemoveLast(2))
										}));
									}
									bool isStarted2 = base.IsStarted;
									if (isStarted2)
									{
										base.RaiseDataReceived(array2);
									}
								}
							}
						}
					}
				}
			}
		}

		/// <inheritdoc />
		// Token: 0x0600106A RID: 4202 RVA: 0x0005F624 File Offset: 0x0005D824
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				SimpleHybirdLock simpleHybirdLock = this.subcriptionHybirdLock;
				if (simpleHybirdLock != null)
				{
					simpleHybirdLock.Dispose();
				}
				List<ModBusMonitorAddress> list = this.subscriptions;
				if (list != null)
				{
					list.Clear();
				}
				SoftBuffer softBuffer = this.coilBuffer;
				if (softBuffer != null)
				{
					softBuffer.Dispose();
				}
				SoftBuffer softBuffer2 = this.inputBuffer;
				if (softBuffer2 != null)
				{
					softBuffer2.Dispose();
				}
				SoftBuffer softBuffer3 = this.registerBuffer;
				if (softBuffer3 != null)
				{
					softBuffer3.Dispose();
				}
				SoftBuffer softBuffer4 = this.inputRegisterBuffer;
				if (softBuffer4 != null)
				{
					softBuffer4.Dispose();
				}
				SerialPort serialPort = this.serialPort;
				if (serialPort != null)
				{
					serialPort.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadInt32(System.String,System.UInt16)" />
		// Token: 0x0600106B RID: 4203 RVA: 0x0005F6C4 File Offset: 0x0005D8C4
		[HslMqttApi("ReadInt32Array", "")]
		public override OperateResult<int[]> ReadInt32(string address, ushort length)
		{
			IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return ByteTransformHelper.GetResultFromBytes<int[]>(this.Read(address, length * base.WordLength * 2), (byte[] m) => transform.TransInt32(m, 0, (int)length));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadUInt32(System.String,System.UInt16)" />
		// Token: 0x0600106C RID: 4204 RVA: 0x0005F720 File Offset: 0x0005D920
		[HslMqttApi("ReadUInt32Array", "")]
		public override OperateResult<uint[]> ReadUInt32(string address, ushort length)
		{
			IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return ByteTransformHelper.GetResultFromBytes<uint[]>(this.Read(address, length * base.WordLength * 2), (byte[] m) => transform.TransUInt32(m, 0, (int)length));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadFloat(System.String,System.UInt16)" />
		// Token: 0x0600106D RID: 4205 RVA: 0x0005F77C File Offset: 0x0005D97C
		[HslMqttApi("ReadFloatArray", "")]
		public override OperateResult<float[]> ReadFloat(string address, ushort length)
		{
			IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return ByteTransformHelper.GetResultFromBytes<float[]>(this.Read(address, length * base.WordLength * 2), (byte[] m) => transform.TransSingle(m, 0, (int)length));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadInt64(System.String,System.UInt16)" />
		// Token: 0x0600106E RID: 4206 RVA: 0x0005F7D8 File Offset: 0x0005D9D8
		[HslMqttApi("ReadInt64Array", "")]
		public override OperateResult<long[]> ReadInt64(string address, ushort length)
		{
			IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return ByteTransformHelper.GetResultFromBytes<long[]>(this.Read(address, length * base.WordLength * 4), (byte[] m) => transform.TransInt64(m, 0, (int)length));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadUInt64(System.String,System.UInt16)" />
		// Token: 0x0600106F RID: 4207 RVA: 0x0005F834 File Offset: 0x0005DA34
		[HslMqttApi("ReadUInt64Array", "")]
		public override OperateResult<ulong[]> ReadUInt64(string address, ushort length)
		{
			IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return ByteTransformHelper.GetResultFromBytes<ulong[]>(this.Read(address, length * base.WordLength * 4), (byte[] m) => transform.TransUInt64(m, 0, (int)length));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadDouble(System.String,System.UInt16)" />
		// Token: 0x06001070 RID: 4208 RVA: 0x0005F890 File Offset: 0x0005DA90
		[HslMqttApi("ReadDoubleArray", "")]
		public override OperateResult<double[]> ReadDouble(string address, ushort length)
		{
			IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return ByteTransformHelper.GetResultFromBytes<double[]>(this.Read(address, length * base.WordLength * 4), (byte[] m) => transform.TransDouble(m, 0, (int)length));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.Int32[])" />
		// Token: 0x06001071 RID: 4209 RVA: 0x0005F8EC File Offset: 0x0005DAEC
		[HslMqttApi("WriteInt32Array", "")]
		public override OperateResult Write(string address, int[] values)
		{
			IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return this.Write(address, byteTransform.TransByte(values));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.UInt32[])" />
		// Token: 0x06001072 RID: 4210 RVA: 0x0005F91C File Offset: 0x0005DB1C
		[HslMqttApi("WriteUInt32Array", "")]
		public override OperateResult Write(string address, uint[] values)
		{
			IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return this.Write(address, byteTransform.TransByte(values));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.Single[])" />
		// Token: 0x06001073 RID: 4211 RVA: 0x0005F94C File Offset: 0x0005DB4C
		[HslMqttApi("WriteFloatArray", "")]
		public override OperateResult Write(string address, float[] values)
		{
			IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return this.Write(address, byteTransform.TransByte(values));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.Int64[])" />
		// Token: 0x06001074 RID: 4212 RVA: 0x0005F97C File Offset: 0x0005DB7C
		[HslMqttApi("WriteInt64Array", "")]
		public override OperateResult Write(string address, long[] values)
		{
			IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return this.Write(address, byteTransform.TransByte(values));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.UInt64[])" />
		// Token: 0x06001075 RID: 4213 RVA: 0x0005F9AC File Offset: 0x0005DBAC
		[HslMqttApi("WriteUInt64Array", "")]
		public override OperateResult Write(string address, ulong[] values)
		{
			IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return this.Write(address, byteTransform.TransByte(values));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.Double[])" />
		// Token: 0x06001076 RID: 4214 RVA: 0x0005F9DC File Offset: 0x0005DBDC
		[HslMqttApi("WriteDoubleArray", "")]
		public override OperateResult Write(string address, double[] values)
		{
			IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
			return this.Write(address, byteTransform.TransByte(values));
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadInt32Async(System.String,System.UInt16)" />
		// Token: 0x06001077 RID: 4215 RVA: 0x0005FA0C File Offset: 0x0005DC0C
		[DebuggerStepThrough]
		public override Task<OperateResult<int[]>> ReadInt32Async(string address, ushort length)
		{
			ModbusTcpServer.<ReadInt32Async>d__70 <ReadInt32Async>d__ = new ModbusTcpServer.<ReadInt32Async>d__70();
			<ReadInt32Async>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<int[]>>.Create();
			<ReadInt32Async>d__.<>4__this = this;
			<ReadInt32Async>d__.address = address;
			<ReadInt32Async>d__.length = length;
			<ReadInt32Async>d__.<>1__state = -1;
			<ReadInt32Async>d__.<>t__builder.Start<ModbusTcpServer.<ReadInt32Async>d__70>(ref <ReadInt32Async>d__);
			return <ReadInt32Async>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadUInt32Async(System.String,System.UInt16)" />
		// Token: 0x06001078 RID: 4216 RVA: 0x0005FA60 File Offset: 0x0005DC60
		[DebuggerStepThrough]
		public override Task<OperateResult<uint[]>> ReadUInt32Async(string address, ushort length)
		{
			ModbusTcpServer.<ReadUInt32Async>d__71 <ReadUInt32Async>d__ = new ModbusTcpServer.<ReadUInt32Async>d__71();
			<ReadUInt32Async>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<uint[]>>.Create();
			<ReadUInt32Async>d__.<>4__this = this;
			<ReadUInt32Async>d__.address = address;
			<ReadUInt32Async>d__.length = length;
			<ReadUInt32Async>d__.<>1__state = -1;
			<ReadUInt32Async>d__.<>t__builder.Start<ModbusTcpServer.<ReadUInt32Async>d__71>(ref <ReadUInt32Async>d__);
			return <ReadUInt32Async>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadFloatAsync(System.String,System.UInt16)" />
		// Token: 0x06001079 RID: 4217 RVA: 0x0005FAB4 File Offset: 0x0005DCB4
		[DebuggerStepThrough]
		public override Task<OperateResult<float[]>> ReadFloatAsync(string address, ushort length)
		{
			ModbusTcpServer.<ReadFloatAsync>d__72 <ReadFloatAsync>d__ = new ModbusTcpServer.<ReadFloatAsync>d__72();
			<ReadFloatAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<float[]>>.Create();
			<ReadFloatAsync>d__.<>4__this = this;
			<ReadFloatAsync>d__.address = address;
			<ReadFloatAsync>d__.length = length;
			<ReadFloatAsync>d__.<>1__state = -1;
			<ReadFloatAsync>d__.<>t__builder.Start<ModbusTcpServer.<ReadFloatAsync>d__72>(ref <ReadFloatAsync>d__);
			return <ReadFloatAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadInt64Async(System.String,System.UInt16)" />
		// Token: 0x0600107A RID: 4218 RVA: 0x0005FB08 File Offset: 0x0005DD08
		[DebuggerStepThrough]
		public override Task<OperateResult<long[]>> ReadInt64Async(string address, ushort length)
		{
			ModbusTcpServer.<ReadInt64Async>d__73 <ReadInt64Async>d__ = new ModbusTcpServer.<ReadInt64Async>d__73();
			<ReadInt64Async>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<long[]>>.Create();
			<ReadInt64Async>d__.<>4__this = this;
			<ReadInt64Async>d__.address = address;
			<ReadInt64Async>d__.length = length;
			<ReadInt64Async>d__.<>1__state = -1;
			<ReadInt64Async>d__.<>t__builder.Start<ModbusTcpServer.<ReadInt64Async>d__73>(ref <ReadInt64Async>d__);
			return <ReadInt64Async>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadUInt64Async(System.String,System.UInt16)" />
		// Token: 0x0600107B RID: 4219 RVA: 0x0005FB5C File Offset: 0x0005DD5C
		[DebuggerStepThrough]
		public override Task<OperateResult<ulong[]>> ReadUInt64Async(string address, ushort length)
		{
			ModbusTcpServer.<ReadUInt64Async>d__74 <ReadUInt64Async>d__ = new ModbusTcpServer.<ReadUInt64Async>d__74();
			<ReadUInt64Async>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ulong[]>>.Create();
			<ReadUInt64Async>d__.<>4__this = this;
			<ReadUInt64Async>d__.address = address;
			<ReadUInt64Async>d__.length = length;
			<ReadUInt64Async>d__.<>1__state = -1;
			<ReadUInt64Async>d__.<>t__builder.Start<ModbusTcpServer.<ReadUInt64Async>d__74>(ref <ReadUInt64Async>d__);
			return <ReadUInt64Async>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.ReadDoubleAsync(System.String,System.UInt16)" />
		// Token: 0x0600107C RID: 4220 RVA: 0x0005FBB0 File Offset: 0x0005DDB0
		[DebuggerStepThrough]
		public override Task<OperateResult<double[]>> ReadDoubleAsync(string address, ushort length)
		{
			ModbusTcpServer.<ReadDoubleAsync>d__75 <ReadDoubleAsync>d__ = new ModbusTcpServer.<ReadDoubleAsync>d__75();
			<ReadDoubleAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<double[]>>.Create();
			<ReadDoubleAsync>d__.<>4__this = this;
			<ReadDoubleAsync>d__.address = address;
			<ReadDoubleAsync>d__.length = length;
			<ReadDoubleAsync>d__.<>1__state = -1;
			<ReadDoubleAsync>d__.<>t__builder.Start<ModbusTcpServer.<ReadDoubleAsync>d__75>(ref <ReadDoubleAsync>d__);
			return <ReadDoubleAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.WriteAsync(System.String,System.Int32[])" />
		// Token: 0x0600107D RID: 4221 RVA: 0x0005FC04 File Offset: 0x0005DE04
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, int[] values)
		{
			ModbusTcpServer.<WriteAsync>d__76 <WriteAsync>d__ = new ModbusTcpServer.<WriteAsync>d__76();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.values = values;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<ModbusTcpServer.<WriteAsync>d__76>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.WriteAsync(System.String,System.UInt32[])" />
		// Token: 0x0600107E RID: 4222 RVA: 0x0005FC58 File Offset: 0x0005DE58
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, uint[] values)
		{
			ModbusTcpServer.<WriteAsync>d__77 <WriteAsync>d__ = new ModbusTcpServer.<WriteAsync>d__77();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.values = values;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<ModbusTcpServer.<WriteAsync>d__77>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.WriteAsync(System.String,System.Single[])" />
		// Token: 0x0600107F RID: 4223 RVA: 0x0005FCAC File Offset: 0x0005DEAC
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, float[] values)
		{
			ModbusTcpServer.<WriteAsync>d__78 <WriteAsync>d__ = new ModbusTcpServer.<WriteAsync>d__78();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.values = values;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<ModbusTcpServer.<WriteAsync>d__78>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.WriteAsync(System.String,System.Int64[])" />
		// Token: 0x06001080 RID: 4224 RVA: 0x0005FD00 File Offset: 0x0005DF00
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, long[] values)
		{
			ModbusTcpServer.<WriteAsync>d__79 <WriteAsync>d__ = new ModbusTcpServer.<WriteAsync>d__79();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.values = values;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<ModbusTcpServer.<WriteAsync>d__79>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.WriteAsync(System.String,System.UInt64[])" />
		// Token: 0x06001081 RID: 4225 RVA: 0x0005FD54 File Offset: 0x0005DF54
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, ulong[] values)
		{
			ModbusTcpServer.<WriteAsync>d__80 <WriteAsync>d__ = new ModbusTcpServer.<WriteAsync>d__80();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.values = values;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<ModbusTcpServer.<WriteAsync>d__80>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.WriteAsync(System.String,System.Double[])" />
		// Token: 0x06001082 RID: 4226 RVA: 0x0005FDA8 File Offset: 0x0005DFA8
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, double[] values)
		{
			ModbusTcpServer.<WriteAsync>d__81 <WriteAsync>d__ = new ModbusTcpServer.<WriteAsync>d__81();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.values = values;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<ModbusTcpServer.<WriteAsync>d__81>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06001083 RID: 4227 RVA: 0x0005FDFA File Offset: 0x0005DFFA
		public override string ToString()
		{
			return string.Format("ModbusTcpServer[{0}]", base.Port);
		}

		// Token: 0x0400043D RID: 1085
		private List<ModBusMonitorAddress> subscriptions;

		// Token: 0x0400043E RID: 1086
		private SimpleHybirdLock subcriptionHybirdLock;

		// Token: 0x0400043F RID: 1087
		private SerialPort serialPort;

		// Token: 0x04000440 RID: 1088
		private SoftBuffer coilBuffer;

		// Token: 0x04000441 RID: 1089
		private SoftBuffer inputBuffer;

		// Token: 0x04000442 RID: 1090
		private SoftBuffer registerBuffer;

		// Token: 0x04000443 RID: 1091
		private SoftBuffer inputRegisterBuffer;

		// Token: 0x04000444 RID: 1092
		private const int DataPoolLength = 65536;

		// Token: 0x04000445 RID: 1093
		private int station = 1;
	}
}
